package com.cupk.questionnaire.service;

import com.cupk.questionnaire.dao.TemplateQuestionDao;
import com.cupk.questionnaire.dao.TemplateQuestionOptionDao;
import com.cupk.questionnaire.dto.TemplateQuestionDTO;
import com.cupk.questionnaire.entity.TemplateQuestion;
import com.cupk.questionnaire.entity.TemplateQuestionOption;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
@Slf4j
@Transactional
public class TemplateQuestionService {
    private final TemplateQuestionDao questionDao;
    private final TemplateQuestionOptionDao questionOptionDao;

    @Autowired
    public TemplateQuestionService(TemplateQuestionDao questionDao,
                                   TemplateQuestionOptionDao questionOptionDao) {
        this.questionDao = questionDao;
        this.questionOptionDao = questionOptionDao;
    }


    public int addQuestion(TemplateQuestion templateQuestion){
        return questionDao.add(templateQuestion);
    }
    public TemplateQuestion getById(int id){
        return questionDao.get(id);
    }
    public List<TemplateQuestion> getByTemplateId(long templateId){
        return questionDao.getByTemplateId(templateId);
    }
    public List<TemplateQuestionDTO> getDetailByTemplate(long templateId){
        List<TemplateQuestionDTO> list = null;
        List<TemplateQuestion> questions = getByTemplateId(templateId);
        if(questions !=null){
            list = new ArrayList<>();
            for(TemplateQuestion q:questions){
                TemplateQuestionDTO questionDTO = new TemplateQuestionDTO();
                questionDTO.setId(q.getId());
                questionDTO.setTemplateId(templateId);
                questionDTO.setQuestionOrder(q.getQuestionOrder());
                questionDTO.setQuestionNum(q.getQuestionNum());
                questionDTO.setQuestionTitle(q.getQuestionTitle());
                questionDTO.setQuestionType(q.getQuestionType());
                questionDTO.setInputPlaceholder(q.getInputPlaceholder());
                List<TemplateQuestionOption> questionOptions = questionOptionDao.getByQuestionId(q.getId());
                questionOptions.sort(Comparator.comparing(TemplateQuestionOption::getOptionOrder));
                questionDTO.setQuestionOptions(questionOptions);
                list.add(questionDTO);
            }
            list.sort(Comparator.comparing(TemplateQuestionDTO::getQuestionOrder));
        }
        return list;
    }

    public void updateQuestion(TemplateQuestion q){
        int n = questionDao.update(q);
        if(n < 1)
            log.warn("更新问题失败！id="+q.getId());
    }

    public void deleteQuestionById(long id){
        questionOptionDao.deleteByQuestionId(id);
        questionDao.delete(id);
    }

    public int deleteQuestionByTemplateId(long templateId){
        List<TemplateQuestion> questions = questionDao.getByTemplateId(templateId);
        if(questions != null && !questions.isEmpty()){
            for(TemplateQuestion q:questions){
                questionOptionDao.deleteByQuestionId(q.getId());
            }
        }
        return questionDao.deleteByTemplateId(templateId);
    }
}
